package com.xhwl.centiotdata.service.integrateconfig;

import com.xhwl.centiotdata.pojo.dto.passengerflow.PassengerFlowIntegrationConfigDTO;
import com.xhwl.common.enums.CategoryEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class PassengerFlowIntergrationConfig extends IntegrationConfig<Integer, List<PassengerFlowIntegrationConfigDTO>> {

    private Map<Integer, List<PassengerFlowIntegrationConfigDTO>> passengerFlowIntegrationMap = new HashMap<>();

    public Map<Integer, List<PassengerFlowIntegrationConfigDTO>> getIntegrationConfig() {
        return passengerFlowIntegrationMap;
    }

    @Override
    public List<PassengerFlowIntegrationConfigDTO> getIntegrationConfig(Integer projectId) {
        return passengerFlowIntegrationMap.get(projectId);
    }

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handlePassengerFlowIntegrationConfig();
    }

    private void handlePassengerFlowIntegrationConfig() {
        passengerFlowIntegrationMap = IntegrationConfig.integrationConfigList.stream()
                .filter(deviceProjSupplierCategory -> CategoryEnum.PASSENGER_FLOW_SYSTEM.getId().equals(deviceProjSupplierCategory.getProdCategoryId()))
                .map(PassengerFlowIntegrationConfigDTO::toPassengerFlow)
                .collect(Collectors.groupingByConcurrent(PassengerFlowIntegrationConfigDTO::getProjectId));

    }
}
